<?php

class Admin_UserGroupController extends Zend_Controller_Action
{

    private $idcat = null;

    private $_model = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->_model = new Application_Model_DbTable_UserGroup();
        
    }

    public function indexAction()
    {
        // action body
        
    }

    public function listAction()
    {
        
        $do = $this->_request->getParam("do");
        if($do=="save"){
            $id = $this->_request->getParam("id");
            $group_name = $this->_request->getParam("group_name");
            $result=0;
            if($group_name!=""){                
                $result = $this->_model->update(array("groups_name"=>$group_name),"idgroups=$id");
            }
            $this->_helper->json->sendJson($result);
        }
        
        if($do=="add"){
            $group_name = $this->_request->getParam("group_name");
            $result = 0;
            if($group_name!="")
            {
                $row = $this->_model->createRow(array("groups_name"=>$group_name));
                $idgroup = $row->save();
                $mdAction = new Application_Model_DbTable_Action();
                $actions = $mdAction->fetchAll();
                $mdRule = new Application_Model_DbTable_Rule();
                foreach($actions as $action)
                {
                    $row = $mdRule->createRow(array("actions_id"=>$action->idactions,"groups_id"=>$idgroup));
                    $row->save();
                }                            
            }
        }
        
        $choice = $this->_request->getParam("choice");
        if(!empty($choice)){
            $idgroup = intval($this->_request->getParam("id"));
            $rowgroup = $this->_model->find($idgroup)->current();
            $n_status = ($rowgroup->groups_status)?0:1;
            if($choice=="multi"){
                $result = $this->_model->update(array("groups_status"=>$n_status),"idgroups=$idgroup");
                $this->_helper->json->sendJson($result);
            }
        }
        
        $do = $this->_request->getParam("do");
        if($do=="del"){
            $id = $this->_request->getParam("id");
            
            $num_users = $this->_model->find($id)->current()
                              ->findDependentRowset("Application_Model_DbTable_User")->count();
            $result = 0;
            if($num_users<=0){                  
                $result = $this->_model->delete("idgroups=$id AND idgroups!=1");
            }
            
            $this->_helper->json->sendJson($result);
        }
        // action body
         $sortby = $this->_request->getParam("sortby",null);
         $sorttype = $this->_request->getParam("sorttype", "asc");
         $filter_field = $this->_request->getParam("filter_field", null);
         $filter_value = $this->_request->getParam("filter_value");
         $this->view->sort = array("by"=>$sortby, "type"=>$sorttype);
         
         $filter = null;
         if(!empty($filter_field))
         {
            $filter[$filter_field] = $filter_value;
         }
         $adapter = $this->_model->fetchPaginatorAdapter($filter,$sortby,$sorttype);
         $paginator = new Zend_Paginator($adapter);
         
         //Show 10 dòng trên 1 trang
         $paginator->setItemCountPerPage(10);
         $page = $this->_request->getParam("page",1);
         $paginator->setCurrentPageNumber($page);
         $this->view->datas = $paginator;
        $this->_helper->layout()->disableLayout();
        $this->_response->clearBody();
    }
    
    public function permissionAction()
    {        
         $datas = array();
         $idgroup = $this->_request->getParam("idgroup",null);
         if($idgroup!=null)
         {
            $group = $this->_model->find($idgroup)->current();
            $mdRules = new Application_Model_DbTable_Rule();
            //$rules = $group->findDependentRowset("Application_Model_DbTable_Rule");
            $select = $mdRules->select();
            $select->setIntegrityCheck(false)
                              ->from(array("r"=>"rules", array("actions_id"=>"action_id","groups_id"=>"groups_id","status"=>"status")))
                              ->join(array("act"=>"actions"),"act.idactions=r.actions_id",array("action_name"=>"display_name"))
                              ->join(array("ctrl"=>"controllers"),"ctrl.idcontrollers=act.controllers_id",array("controller_name"=>"display_name"))
                              ->where("r.groups_id=?",$idgroup)
                              ->order("act.controllers_id")
                              ->order("act.idactions");
            $rules = $mdRules->fetchAll($select);
            
            $this->view->group = $group;
            $this->view->rules = $rules;
         }  
         else
         {
            $this->_redirect($this->view->url(array(
                "module"=>"admin",
                "controller"=>"user-group",
                "action"=>"index"
            ), null, true));
         } 
    }
    
    public function changePerAction()
    {
        $action = $this->_request->getParam("act", null);
        $group = $this->_request->getParam("group", null);
        if($group==1)
        {
            exit(0);
        }
        if($action!=null && $group!= null)
        {
            $mdRule = new Application_Model_DbTable_Rule();
            $row = $mdRule->find($group,$action)->current();
            $row->status = ($row->status)?0:1;
            $result = $row->save();
            $this->_helper->json->sendJson($result);
        }
    }

}













